package com.springboot.demo.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.springboot.demo.entity.Admin;
import com.springboot.demo.entity.Reader;
import com.springboot.demo.service.BaseSetupService;
import com.springboot.demo.service.ReaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
public class ReaderController {

    @Autowired
    private ReaderService readerService;
    @Autowired
    private BaseSetupService baseSetupService;

    @RequestMapping("/readerlogin")
    public String login(Model model,HttpServletRequest request) {
        request.getSession().invalidate();
        return "reader_login";
    }

    @RequestMapping("/welcome")
    public String welcome(Model model,HttpServletRequest request){
        request.getSession().invalidate();
        return "reader_welcome";
    }

    @RequestMapping("/readerRegister")
    public String readerResister(Model model,HttpServletRequest request){
        request.getSession().invalidate();
        return "reader_register";
    }
    @RequestMapping("/payDep")
    public String payDep(Model model,HttpServletRequest request){
        request.getSession().invalidate();
        return "reader_paydeposit";
    }

    @RequestMapping(value = "/read_register",method = RequestMethod.POST)
    @ResponseBody
    public String registerReader(String name,String phone, String email, String password){
        Reader reader = readerService.getReaderByPhone(phone);
        if(reader != null && reader.getRegister().equals("registering") && reader.getDeposit() == 300) {
            //已注册并缴费但管理员未同意
            return "no1";
        }else if(reader != null && reader.getRegister().equals("registering") && reader.getDeposit() == 0){
            //注册但未缴费
            return "no2";
        }else if(reader != null && reader.getRegister().equals("yes")){
            //已拥有账户
            return "no3";
        }
        Calendar calendar = Calendar.getInstance();
        String date = readerService.TimeToString(calendar);
        readerService.addReader(phone, name, password, email, date,0,0,"registering");
        return "yes";
    }

    @RequestMapping(value = "/payDeposit",method = RequestMethod.POST)
    @ResponseBody
    public String payDeposit(String phone){
        int deposit = baseSetupService.getDepositValue();
        readerService.payDeposit(deposit,phone);
        return "yes";
    }

    @RequestMapping(value = "/vertify",method = RequestMethod.POST)
    @ResponseBody
    public Reader vertify(String phone, String password){
        final Reader login = readerService.login(phone, password);
        return login;
    }

    @RequestMapping("/readerchangeinfo")
    public String rci(String username,Model model) {
        Reader reader = readerService.getReaderByName(username);
        model.addAttribute("user",reader);
        return "reader_changeinfo";
    }
    @RequestMapping("/readerborrowinfo")
    public String bor(String username,Model model) {
        Reader reader = readerService.getReaderByName(username);
        model.addAttribute("user",reader);
        return "reader_borrowinfo";
    }

    @RequestMapping("/readerfinerecord")
    public String readerfine(String username, Model model){
        Reader reader = readerService.getReaderByName(username);
        model.addAttribute("user",reader);
        return "reader_fine_record";
    }
    @RequestMapping("/modifyuserinfo")
    @ResponseBody
    public Map modifyUserInfo(String oname, String opassword, String name, String password){
        Map ret=new HashMap();
        ret.put("success",true);
        ret.put("msg","修改成功");
        if(StringUtils.isEmpty(name)||StringUtils.isEmpty(password)){
            ret.put("success",false);
            ret.put("msg","请输入修改信息");
            return ret;
        }
        if(oname.equals(name)){
            readerService.updateInfo(oname,opassword,name,password);
        }else{
            Reader reader=readerService.getReader(name);
            if(reader!=null){
                ret.put("success",false);
                ret.put("msg","用户已存在");
                return ret;
            }
            readerService.updateInfo(oname,opassword,name,password);
        }
        return ret;
    }
     @PostMapping("/payfine")
     @ResponseBody
    public Map payFine(String phone, int totalFine) {
         Map ret=new HashMap();
         Reader reader = readerService.getReaderByPhone(phone);
         if(reader.getDeposit() < totalFine)
             ret.put("success",false);
         else
             ret.put("success",true);
         ret.put("msg","支付失败,您的保证金余额不足");
        System.out.println("~~~~~~~~~~~~~~~~~~~pay!");
        readerService.payReaderFine(phone,totalFine);
        readerService.updatefine(phone);
        return ret;
    }

}


